package com.guodi.map.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guodi.common.utils.UuidUtil;
import com.guodi.core.minio.util.MinioUtil;
import com.guodi.map.entity.MapScreenshot;
import com.guodi.map.mapper.MapScreenshotMapper;
import com.guodi.map.service.IMapScreenshotService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Service
public class MapScreenshotServiceImpl extends ServiceImpl<MapScreenshotMapper, MapScreenshot> implements IMapScreenshotService {
    @Override
    public void save(MapScreenshot screenshot, MultipartFile file) throws IOException {
        screenshot.setId(UuidUtil.get32UUID());
        if(StringUtils.isBlank(screenshot.getName())) {
            screenshot.setName(file.getOriginalFilename());
        }
        String path = MinioUtil.uploadFile(file.getInputStream(),"map_screenshot",file.getOriginalFilename());
        screenshot.setPath(path);
        screenshot.setSize(file.getSize());
        this.save(screenshot);
    }

    @Override
    public boolean deleteById(String id) {
        MapScreenshot screenshot = this.getById(id);
        String path = screenshot.getPath();
        MinioUtil.removeFile(path);
        this.removeById(id);
        return true;
    }
}
